<template>
  <div class="onload">
    <div class="left">
      <img class="circle" src="@/assets/onload.png" alt="" />
      <img src="@/assets/logo1.png" alt="" class="logo" />
    </div>
    <div class="right">
      <div class="onloadbox">
        <div class="inputbox">
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="150px"
            class="demo-ruleForm"
          >
            <el-form-item label="用户名/手机号：" prop="account">
              <el-input v-model="ruleForm.account"></el-input>
            </el-form-item>
            <el-form-item label="密码：" prop="pwd">
              <el-input
                v-model="ruleForm.pwd"
                type="password"
                show-password
                @keyup.enter.native="submitForm('ruleForm')"
              ></el-input>
            </el-form-item>
            <!-- <el-form-item>
             <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
          </el-form-item> -->
          </el-form>
        </div>
        <div class="btn">
          <el-button
            type="primary"
            v-preventReClick
            @click="submitForm('ruleForm')"
            >登录</el-button
          >
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { login } from '@/api/login.js';
// import { mapState } from 'vuex';
// import { mapMutations } from 'vuex';
export default {
  name: 'onload',
  data() {
    return {
      ruleForm: {
        account: '',
        pwd: ''
      },
      rules: {
        account: [
          { required: true, message: '请输入用户名/手机号', trigger: 'blur' }
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        pwd: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      },
      departmentIdList: []
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          // alert('submit!');
          // console.log(this.ruleForm);
          const res = await login(JSON.stringify(this.ruleForm));
          console.log(res);
          if (res.code === 200) {
            res.data.departmentList.forEach(item => {
              this.departmentIdList.push(item.departmentId);
            });
            this.$message({
              showClose: true,
              message: '登录成功！',
              type: 'success'
            });
            sessionStorage.setItem('userid', res.data.id);
            sessionStorage.setItem('usertype', res.data.type);
            sessionStorage.setItem('usertoken', res.data.token);
            sessionStorage.setItem('username', res.data.name);
            sessionStorage.setItem('departmentIdList', JSON.stringify(this.departmentIdList));
            sessionStorage.setItem('avatar', res.data.avatar);
            sessionStorage.setItem('userMenuList', JSON.stringify(res.data.menurightVos));
            if (res.data.type === 3) {
              setTimeout(() => {
                this.$router.replace('/lesson');
              }, 1000);
            } else {
              setTimeout(() => {
                this.$router.replace('/board');
              }, 1000);
            }
          } else {
            this.$message({
              showClose: true,
              message: `${res.msg}`,
              type: 'error'
            });
          }
        } else {
          this.$message({
            showClose: true,
            message: '登录失败！请正确填写信息。',
            type: 'error'
          });
          return false;
        }
      });
    }
  }
};
</script>

<style lang="less" scoped>
.onload {
  width: 100vw;
  height: 100vh;
  background: #08051c;
  overflow: hidden;
  .left {
    width: 50%;
    position: relative;
    display: flex;
    // overflow: hidden;
    .logo {
      // height: 250px;
      height: 193px;
      position: absolute;
      // top: 350px;
      top: calc(100% + 390px);
      // left: 100px;
      right: 600px;
    }
    .circle {
      height: 1900px;
      // width: 200vh;
      // height: 1080px;
      // position: fixed;
      position: absolute;
      // top: calc(50% - 470px);
      top: -480px;
      // bottom: -1080px;
      // left: calc(100% - 1650px);
      right: -220px;
      animation: trunaround 6s;
      animation-iteration-count: infinite;
      animation-timing-function: linear;
    }
  }
  .right {
    .onloadbox {
      position: absolute;
      top: calc(50% - 225px);
      right: 15%;
      width: 600px;
      height: 400px;
      border-radius: 8px;
      background: #fff;
      // opacity: 0.9;
      .btn {
        width: calc(100% - 200px);
        margin: 0px 100px;
        .el-button--primary {
          width: 100%;
          background: #2fc0e0;
          border-radius: 5px;
          border: 0;
          box-shadow: 0px 0px 1px 1px #2fc0e0;
          font-size: 18px;
        }
      }
      .inputbox {
        margin: 60px 100px;
        margin-bottom: 0px;
        width: calc(100% - 200px);
        height: calc(100% - 180px);
        // color: transparent;l
        .username,
        .password {
          width: 100%;
          height: 80px;
          margin-bottom: 10px;
          border-bottom: 1px solid #d8d8d8;
          transition: all 0.3s;
          position: relative;
          input {
            position: absolute;
            bottom: 10px;
            border: none;
            outline: none;
            width: 100%;
            font-size: 16px;
            color: #222;
          }
          span {
            position: absolute;
            bottom: 0;
            transition: all 0.1s linear;
            z-index: 5;
          }
        }
        .loadnum {
          cursor: pointer;
          height: 50px;
          width: 100%;
          background: #2fc0e0;
          border-radius: 5px;
          margin-top: 50px;
          text-align: center;
          line-height: 50px;
          color: #fff;
        }
      }
    }
  }
  @keyframes trunaround {
    from {
      transform: rotate(0);
    }
    to {
      transform: rotate(360deg);
    }
  }
}
.el-form.demo-ruleForm {
  padding-top: 30px;
}
.el-form-item {
  // text-align: left;
  margin-bottom: 45px;
}
.el-form-item__label {
  font-size: 16px;
}
</style>
